package com.yssoft.controller.app.sales;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yssoft.entity.core.TenterpriseLicence2;
import com.yssoft.entity.sales.TCustomer;
import com.yssoft.entity.sales.TCustomerAtt;
import com.yssoft.service.sales.DealerService;
import com.yssoft.utils.AppUtil;

@CrossOrigin
@RequestMapping("/app/dealer")
@Controller
public class DealerController {
	private static Logger logger=Logger.getLogger(DealerController.class);
	@Resource
	private DealerService dealerService;
	
	
	@ResponseBody
	@RequestMapping(value = "findPage",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findPage(Integer currentPage, Integer pageSize, @RequestParam  Map<String, Object> param,  String name, HttpServletRequest request) {
		String msg = "经销商列表";
		try{
    		List<Map<String,Object>> pageList = dealerService.findPage(currentPage, pageSize, name, request,param);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "findDetail",method = {RequestMethod.POST,RequestMethod.GET})
	public Object findDetail(Integer id ) {
		String msg = "经销商查看";
    	try{
    		Map<String,Object> custom = dealerService.findDetail(id);
    		return AppUtil.returnMapJson("1", msg+"成功!", custom);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="deleteDealer",method = {RequestMethod.POST,RequestMethod.GET})
	public Object deleteDealer(String ids) {
		String msg = "经销商删除";
        try {
        	String[] idArry = ids.split(",");
        	msg=dealerService.deleteDealer(idArry);
        	if("0".equals(msg)) {
        		msg="经销商删除成功!";
        	}
            return AppUtil.returnMapJson("1",msg, null);
        } catch (Exception e) {
            logger.info(msg+"失败："+e.getMessage());
            return AppUtil.returnMapJson("0", msg+"失败!", null);
        }
	}
	@ResponseBody
	@RequestMapping(value="edit",method = {RequestMethod.POST,RequestMethod.GET})
	public Object save(TCustomer custom,TenterpriseLicence2 tEnterpriseLicence,RedirectAttributes redirectAttributes, HttpServletRequest request,String areaJson) {
		System.out.println(areaJson);
		String msg = "经销商";
        try {
        	if(custom.getId()!=null && 0!=custom.getId()){
        		dealerService.editTCustomer(custom,tEnterpriseLicence,request,areaJson);
    			msg =msg+"修改成功";
    		} else {
    			dealerService.saveTCustomer(custom, tEnterpriseLicence, request,areaJson);
    			msg = msg+"保存成功";
    		}
        	 
            return AppUtil.returnMapJson("1",msg, null);
        } catch (Exception e) {
            logger.info(msg+"失败："+e.getMessage());
            return AppUtil.returnMapJson("0", msg+"失败!", null);
        }
	}
	/**
	 * 图片上传
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "uploadImage", method = RequestMethod.POST)
	public Object uploadImage(HttpServletRequest request) throws Exception {
		try{
			MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
			MultipartFile mFile = mRequest.getFile("img");
			Map<String, Object> fileNames = dealerService.compressUploadedFile(mFile);
			return AppUtil.returnMapJson("1", "上传成功!", fileNames);
		} catch (Exception e) {
			return AppUtil.returnMapJson("0", "上传失败!", null);
		}
	}
}
